import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)
const key = 'token'
const store = new Vuex.Store({
    state(){
        return {
            token:localStorage.getItem('token')?localStorage.getItem('token'):'',
            
            tagsList: []
        }
    },
    getters:{
        getSortage:function(state){
            if(!state.token){
                state.token = JSON.parse(localStorage.getItem(key))
            }
            return state.token
        }
    },
    mutations:{
        $_setStorage(state,value){
            state.token = value
            localStorage.setItem(key,JSON.stringify(value))
        },
        
        addTag(state, payload){
            state.tagsList.push(payload)
        },
        removeTag(state, payload){
            for(let i=0;i<state.tagsList.length;i++){
                if(state.tagsList[i].path === payload){
                    state.tagsList.splice(i,1);
                    break;
                }
            }
        }
    }
})

export default store;